package com.huli.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class GateWayConfig {

    @Bean
    public CorsWebFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("*"); // 允许任何源，或者设置具体的源
        config.addAllowedHeader("*"); // 允许任何头
        config.addAllowedMethod("*"); // 允许任何方法
        config.setAllowCredentials(true);

        config.addAllowedHeader("Authorization");  // 允许的请求头，例如 Authorization

        // 设置允许的请求方法
        config.addAllowedMethod("GET");  // 只允许 GET 方法
        config.addAllowedMethod("POST"); // 只允许 POST 方法
        config.addAllowedMethod("PUT");  // 只允许 PUT 方法

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);

        return new CorsWebFilter(source);
    }
}
